package com.neutech.dao.impl;

import com.neutech.constant.Constant;
import com.neutech.dao.CartDAO;
import com.neutech.entity.Cart;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDAOImpl implements CartDAO {
    @Override
    public Cart getByFoodIdAndUserId(Integer foodId, Integer userID) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Cart cart = null;

        try {
            connection = Constant.DATASOURCE.getConnection();
            String sql = "select * from cart where foodId = ? and userId = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,foodId);
            preparedStatement.setInt(2,userID);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                cart = new Cart();
                cart.setId(resultSet.getInt("Id"));
                cart.setFoodId(resultSet.getInt("foodId"));
                cart.setFoodName(resultSet.getString("foodName"));
                cart.setFoodImg(resultSet.getString("foodImg"));
                cart.setFoodPrice(resultSet.getBigDecimal("foodPrice"));
                cart.setBusinessId(resultSet.getInt("businessId"));
                cart.setUserId(resultSet.getInt("businessId"));
                cart.setUserId(resultSet.getInt("userId"));
                cart.setQuantity(resultSet.getInt("quantity"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return cart;
    }

    @Override
    public int add(Cart cart) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int row = 0;
        try {
            connection = Constant.DATASOURCE.getConnection();
            String sql = "insert into cart(foodId,foodName,foodImg,foodPrice,businessId,userId,quantity) values(?,?,?,?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,cart.getFoodId());
            preparedStatement.setString(2,cart.getFoodName());
            preparedStatement.setString(3,cart.getFoodImg());
            preparedStatement.setBigDecimal(4,cart.getFoodPrice());
            preparedStatement.setInt(5,cart.getBusinessId());
            preparedStatement.setInt(6,cart.getUserId());
            preparedStatement.setInt(7,cart.getQuantity());
            row = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return row;
    }

    @Override
    public int updateQuantity(Cart cart) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int row = 0;
        try {
            connection = Constant.DATASOURCE.getConnection();
            String sql = "update cart set quantity = ? where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,cart.getQuantity());
            preparedStatement.setInt(2,cart.getId());
            row = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return row;
    }

    @Override
    public List<Cart> listByUserId(Integer userId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Cart> cartList =  new ArrayList<>();

        try {
            connection = Constant.DATASOURCE.getConnection();
            String sql = "select * from cart where userId = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,userId);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Cart cart = new Cart();
                cart.setId(resultSet.getInt("Id"));
                cart.setFoodId(resultSet.getInt("foodId"));
                cart.setFoodName(resultSet.getString("foodName"));
                cart.setFoodImg(resultSet.getString("foodImg"));
                cart.setFoodPrice(resultSet.getBigDecimal("foodPrice"));
                cart.setBusinessId(resultSet.getInt("businessId"));
                cart.setUserId(resultSet.getInt("userId"));
                cart.setQuantity(resultSet.getInt("quantity"));

                cartList.add(cart);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (resultSet != null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return cartList;
    }

    @Override
    public Cart getByAndUserId(Integer id, Integer userId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Cart cart = null;

        try {
            connection = Constant.DATASOURCE.getConnection();
            String  sql = "select * from cart where Id = ? and userId = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            preparedStatement.setInt(2,userId);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                cart = new Cart();
                cart.setId(resultSet.getInt("Id"));
                cart.setFoodId(resultSet.getInt("foodId"));
                cart.setFoodName(resultSet.getString("foodName"));
                cart.setFoodImg(resultSet.getString("foodImg"));
                cart.setFoodPrice(resultSet.getBigDecimal("foodPrice"));
                cart.setBusinessId(resultSet.getInt("businessId"));
                cart.setUserId(resultSet.getInt("userId"));
                cart.setQuantity(resultSet.getInt("quantity"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (resultSet != null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return cart;
    }

    @Override
    public int deleteByQuantity() {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        int row=0;
        try {
            connection= Constant.DATASOURCE.getConnection();
            String sql="DELETE FROM cart where quantity<1";
            preparedStatement=connection.prepareStatement(sql);
            row=preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement!=null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return row;
    }
}
